package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.business.BusinessPageDTO;
import com.qk.dto.business.BusinessPoolPageDTO;
import com.qk.dto.business.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessPoolPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 *
 * @author MrZhao
 * @created 2025-10-15 10:54
 * @description
 *  商机Controller
 *
 */
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping("pool")
    public Result getBusinessPoolPageList(BusinessPoolPageDTO dto){
       return Result.success(businessService.getBusinessPoolPageList(dto));
    }


    @PutMapping
    public Result updateBusiness(@RequestBody BusinessUpdateDTO dto) {
        businessService.updateBusiness(dto);
        return  Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(businessService.getBusinessById(id));
    }


    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        businessService.backToPool(id);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }



    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusiness(@PathVariable Integer businessId,@PathVariable Integer userId) {
        if (businessId==null||userId==null){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        businessService.updateById(Business.builder()
                        .id(businessId)
                        .userId(userId)
                        .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                        .updateTime(LocalDateTime.now())
                .build());
        return  Result.success();
    }

    @PostMapping
    public Result saveBusiness(@RequestBody Business business) {
        businessService.saveBusiness(business);
        return  Result.success();
    }

    @GetMapping
    public Result businessPageList(BusinessPageDTO dto) {
        return Result.success( businessService.businessPageList(dto));
    }

}